function GridHeaderCell(headerCellTemplate, gridHeader, headerPositionOrder, sortDataType){
	this.headerCellTemplate = headerCellTemplate;
	this.gridHeader = gridHeader;
	this.headerPositionOrder = headerPositionOrder;
	this.headerCellTemplateParents = new Array();
	this.headerCellTemplateChildren = new Array();
	this.sortDataType = sortDataType;
	this.sortType = null;
	
	this.startDrag = false;
	this.resizeWidth = false;
	this.resizeHeight = false;
	this.startResize = false;
	this.sX = 0;
	
	this.onSort = function(gridHeaderCell){};
	
	this.init();
	
	return this;
}

// ------------------------------------------------------------------------------------------------

GridHeaderCell.prototype.init = function(){
	
	//alert(this.headerCellTemplate.parentElement.parentElement != this.gridHeader.headerContainerTemplate);
	
	//retrieve parents
	if (this.gridHeader!=null && this.gridHeader.headerContainerTemplate != null){
		var parentElement = this.headerCellTemplate.parentElement;
		
		while (parentElement!= null && parentElement.parentElement != this.gridHeader.headerContainerTemplate){
			this.headerCellTemplateParents.push(parentElement);
			
			parentElement = parentElement.parentElement;
		}
	};
	
	
	
	//retrieve children
	if (this.headerCellTemplate != null && this.headerCellTemplate.childNodes.length>0){
		var childElement = this.headerCellTemplate.childNodes[0];
		
		while (childElement != null){
			this.headerCellTemplateChildren.push(childElement);
			childElement = childElement.childNodes[0];
		}
		
		//alert(this.headerCellTemplate.id + " : " + this.headerCellTemplateChildren.length);	
	}
	
	var gridHeaderCell = this;
	
	XBrowserAddHandler(this.headerCellTemplate, "click", function onCLick(e){
		if (gridHeaderCell.startResize==false){
			gridHeaderCell.onSort(gridHeaderCell);
		}
	});

	XBrowserAddHandler(this.headerCellTemplate, "mouseup", function onMouseUp(e){
//		window.status = "Stop Resize";
//	
//		window.status += " Calc new width";
		
		/*
		var eX =  XBrowserEventClientX(e);
		var diffX = eX-gridHeaderCell.sX;
		var wWidth = parseInt(gridHeaderCell.headerCellTemplate.style.width) + diffX;
		window.status = "New Width: " + wWidth;
		gridHeaderCell.headerCellTemplate.style.width = String(wWidth) + "px";
		*/
	
		//gridHeaderCell.startResize = false;
				
		
		for (var gridRow=0; gridRow<gridHeaderCell.gridHeader.grid.gridRows.length; gridRow++){
			gridHeaderCell.gridHeader.grid.gridRows[gridRow].cellElements[gridHeaderCell.headerPositionOrder].style.borderRight = "";
		}		
	});
	

	XBrowserAddHandler(this.headerCellTemplate, "mousedown",  function onMouseDown(e){
		var x = XBrowserEventClientX(e);
		var offLeft = XBrowserOffsetLeft(gridHeaderCell.headerCellTemplate) + XBrowserElementWidth(gridHeaderCell.headerCellTemplate);
		
		if (offLeft-5 <= x && x <= offLeft + 5){
			gridHeaderCell.startResize = true;
			
//			for (var gridRow=0; gridRow<gridHeaderCell.gridHeader.grid.gridRows.length; gridRow++){
//				gridHeaderCell.gridHeader.grid.gridRows[gridRow].cellElements[gridHeaderCell.headerPositionOrder].style.borderRight = "#cdcdcd 2px solid";
//			}
			
			var verticalBar = document.createElement("div");
			
			//verticalBar.setAttribute("z-index", "2000");
			//verticalBar.setAttribute("position", "absolute");
			verticalBar.setAttribute("width", "5px");
			verticalBar.setAttribute("height", "25px");
			verticalBar.setAttribute("background-color", "#ff5500");
			
//			verticalBar.style.position = "absolute";
//			verticalBar.style.width = "5px";
//			verticalBar.style.height = "25px";
			
			
			gridHeaderCell.gridHeader.grid.gridContainer.appendChild(verticalBar);
		}
		
		gridHeaderCell.sX = XBrowserEventClientX(e);
	});
	
	
	XBrowserAddHandler(this.headerCellTemplate, "mousemove",  function onMouseDown(e){
		
	});

//	this.headerCellTemplate.attachEvent("onmousemove", function onMouseMove(e){
//		if (gridHeaderCell.startResize){
//			window.status = "Resize " + '"' + gridHeaderCell.headerCellTemplate.id + '"';
//		}
//	});
	
};

//------------------------------------------------------------------------------------------------

GridHeaderCell.prototype.dataCompaire = function(object1, object2){
	
	var value1 = getAttributeByIndex(object1, this.headerPositionOrder).toUpperCase();
	var value2 = getAttributeByIndex(object2, this.headerPositionOrder).toUpperCase();
	
	if (this.sortDataType==new SortDataType().numeric){
		value1 = Number(value1);
		value2 = Number(value2);
	}
	
	if (this.sortDataType==new SortDataType().date){
		var date1 = new Date();
		value1 = date1.parseFromString("dd/MM/yyyy", value1);
		
		var date2 = new Date();
		value2 = date2.parseFromString("dd/MM/yyyy", value2);
	}
		
	
	switch (this.sortType) {
		case new SortTypes().ascending:
			//Numeric
			if (this.sortDataType==new SortDataType().numeric || this.sortDataType==new SortDataType().date){
				return value1-value2;
			}
	
			//String
			if (this.sortDataType==new SortDataType().alphanumeric){
				if (value1 < value2){
					return -1;
				}
				if (value1 > value2){
					return 1;
				}
			}
			
			
		break;
	
		case new SortTypes().descending: 
			//Numeric	
			if (this.sortDataType==new SortDataType().numeric  || this.sortDataType==new SortDataType().date){
				return value2-value1;
			}
	
			//String
			if (this.sortDataType==new SortDataType().alphanumeric){
				if (value1 < value2){
					return 1;
				}
				if (value1 > value2){
					return -1;
				}
			}
		
			break;
		}

	return 0;	
};

//------------------------------------------------------------------------------------------------

GridHeaderCell.prototype.getHeaderPositionOrder = function(){
	return this.headerPositionOrder;
};